import heapq
class Solution(object):
    def assignTasks(self, servers, tasks):
        servers = [[s, index] for index, s in enumerate(servers)]
        heapq.heapify(servers)
        n = len(tasks)
        ans = [0] * n
        used = []
        time = 0
        for index, task in enumerate(tasks):
            while used and used[0][0] <= time:
                _, w, id = heapq.heappop(used)
                heapq.heappush(servers, [w, id])
            if servers:
                w, id = heapq.heappop(servers)
                ans[index] = id
                heapq.heappush(used, [time + task, w, id])
            else:
                time, w, id = heapq.heappop(used)
                ans[index] = id
                heapq.heappush(used, [time + task, w, id])
            time = max(index + 1, time)
        return ans

